<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="课表编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courseScheduleCode">
              <a-input v-model="model.courseScheduleCode" placeholder="请输入课表编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课表名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courseScheduleName">
              <a-input v-model="model.courseScheduleName" placeholder="请输入课表名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课表类型，课表应用对象类型" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courseScheduleTtype">
              <j-dict-select-tag @change="handleChangeObjType" type="list" v-model="model.courseScheduleTtype"
                                 dictCode="course_target_obj_ttype" placeholder="请选择课表类型，课表应用对象类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="目标对象，该课表应用对象" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="targetObjId">
              <!--              <j-dict-select-tag type="list" v-model="model.targetObjId" dictCode="" placeholder="请选择目标对象" />-->
              <a-select v-model="model.targetObjId" placeholder="请输入目标对象"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in objectList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单双周标识" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courseScheduleIzSingle">
              <j-dict-select-tag type="list" v-model="model.courseScheduleIzSingle" dictCode="common_iz_single"
                                 placeholder="请选择单双周标识" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="课表状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courseScheduleStatus">
              <j-dict-select-tag type="list" v-model="model.courseScheduleStatus" dictCode="course_arrange_status"
                                 placeholder="请选择课表状态：" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izOpen">
              <j-dict-select-tag type="list" v-model="model.izOpen" dictCode="iz_open" placeholder="" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import {
  getAllClass,
  getClassroomDataList,
  getClassStudentQueryAll,
  getSubjectList,
  getTeacherList
} from '@api/commonAPI'

export default {
  name: 'CouCourseScheduleForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        targetObjId: [
          { required: true, message: '请输入目标对象id(班级id(默认)，场地id，老师id，科目id,学生id)，表示该课表应用对象!' }
        ],
        izOpen: [
          { required: true, message: '请输入是否启用（0否停用，1是启用）!' }
        ]
      },
      objectList: [],
      url: {
        add: '/coucourseschedule/couCourseSchedule/add',
        edit: '/coucourseschedule/couCourseSchedule/edit',
        queryById: '/coucourseschedule/couCourseSchedule/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    handleChangeObjType() {
      let type = this.model.courseScheduleTtype
      this.objectList = []
      switch (type) {
        case '1':
          this.getClassesList()
          break
        case '2' :
          this.getClassroomList()
          break
        case '3':
          this.getTeacherList()
          break
        case '4':
          this.getSubjectList()
          break
        case '5':
          this.getStudentList()
          break
        default :
          this.$message.error('获取初始化数组出错')
      }
    },
    async getClassesList() {
      let res = await getAllClass()
      if (res.success) {
        res.result.forEach(item => {
          this.objectList.push({
            label: item.className,
            value: item.id
          })
        })
      } else {
        this.$message.error('获取初始化数组出错')
      }
    },
    async getClassroomList() {
      let res = await getClassroomDataList()
      if (res.success) {
        res.result.forEach(item => {
          this.objectList.push({
            label: item.classroomName,
            value: item.id
          })
        })
      } else {
        this.$message.error('获取初始化数组出错')
      }
    },
    async getTeacherList() {
      let res = await getTeacherList()
      if (res.success) {
        res.result.forEach(item => {
          this.objectList.push({
            label: item.teacherName,
            value: item.teacherId
          })
        })
      } else {
        this.$message.error('获取初始化数组出错')
      }
    },
    async getSubjectList() {
      let res = await getSubjectList()
      if (res.success) {
        res.result.forEach(item => {
          this.objectList.push({
            label: item.subjectName,
            value: item.subjectId
          })
        })
      } else {
        this.$message.error('获取初始化数组出错')
      }
    },
    async getStudentList() {
      let res = await getClassStudentQueryAll()
      if (res.success) {
        res.result.forEach(item => {
          this.objectList.push({
            label: item.studentName,
            value: item.id
          })
        })
      } else {
        this.$message.error('获取初始化数组出错')
      }
    }
  }
}
</script>